// packageJ/speed_up_index/speed_up_index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    name: "",
    user: [],
    statistic: [],
    listInfo: [],
    diy_name: "",

    page: 1,
    total_page: 0,
    isLoadMore: true,
    sort_filed: 'time',
    sort: 'asc',
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.name) {
      this.setData({
        name: options.name
      })
    }
    this.init();
    this.getData();
    this.getList();
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#ff8924',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log(this.data.name)
    let basic_info = wx.getStorageSync('yz_basic_info').plugin_setting;
    if (this.data.name == 'reward_pool_dividend') {
      wx.setNavigationBarTitle({
        title: basic_info?.reward_pool_dividend?.diy_name || '奖金池分红',
      })
    } else {
      wx.setNavigationBarTitle({
        title: basic_info?.quicken_income?.diy_name || '加速收益分红',
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData()
    } else {
      console.log('没有更多')
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onChange(e) {
    this.setData({
      active: e.detail.index
    })
    this.init();
    this.getList();
  },
  init() {
    this.setData({
      page: 1,
      total_page: 0,
      listInfo: [],
      isLoadMore: true
    })
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.quicken-income.frontend.controllers.index.index");
    if (this.data.name == "reward_pool_dividend") {
      urlStr = app.getNetAddresss("plugin.reward-pool-dividend.api.index.index");
    }
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          console.log(res)
          let {
            user,
            statistic
          } = res.data;
          this.setData({
            user,
            statistic
          })
        } else {
          app.tips(res.msg)
        }
      }
    });
  },
  getList() {
    let urls = ["plugin.quicken-income.frontend.controllers.index.equity", "plugin.quicken-income.frontend.controllers.index.equity-detail"];
    if (this.data.name == "reward_pool_dividend") {
      urls = ["plugin.reward-pool-dividend.api.index.reward-log"];
    }
    console.log(urls[this.data.active])
    let urlStr = app.getNetAddresss(urls[this.data.active]);
    app._getNetWork({
      url: urlStr,
      data: {
        page: this.data.page,
        sort_filed: this.data.sort_filed, 
        sort: this.data.sort
      },
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          let {
            data,
            last_page
          } = this.data.name == "reward_pool_dividend" ? res.data : res.data.list;
          this.data.isLoadMore = true;
          this.data.total_page = last_page;
          this.setData({
            listInfo: data
          })
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
        } else {
          app.tips(res.msg)
        }
      }
    });
  },
  getMoreData() {
    let urls = ["plugin.quicken-income.frontend.controllers.index.equity", "plugin.quicken-income.frontend.controllers.index.equity-detail"];
    if (this.data.name == "reward_pool_dividend") {
      urls = ["plugin.reward-pool-dividend.api.index.reward-log"];
    }
    let urlStr = app.getNetAddresss(urls[this.data.active]);
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page,
          sort_filed: this.data.sort_filed, 
          sort: this.data.sort
        },
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            let {
              data
            } = this.data.name == "reward_pool_dividend" ? res.data : res.data.list;
            this.setData({
              listInfo: this.data.listInfo.concat(data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  toggle(e){
    this.setData({
      sort_filed: e.currentTarget.dataset.type,
      sort: this.data.sort == 'asc'?'desc':'asc'
    });
    this.init();
    this.getList();
  }
})